home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10390 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: news.sover.net!news
  2. From: mountain@sover.net (Steve Mount)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Empty String function
  5. Date: 17 Mar 1996 15:02:16 GMT
  6. Organization: SoVerNet, Inc.
  7. Message-ID: <4ih9ho$680@thrush.sover.net>
  8. References: <1996Mar14.205741.19178@vtf.idx.com> <826901446snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: pm0a6.mid.sover.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <826901446snz@genesis.demon.co.uk>, fred@genesis.demon.co.uk says...
  15. >
  16. >In article <1996Mar14.205741.19178@vtf.idx.com>
  17. >           sjjm@hawkeye.idx.com "Steve Mount" writes:
  18. >
  19. >>int zemp(char *ptxt, int len)
  20. >>{
  21. >>static int i;
  22. >
  23. >Why make i static? It would be better off as an auto variable.
  24.  
  25. The compiler docs for my compiler says that this allows the compiler
  26. to optimize this variable - it does not need to recreate it each time
  27. it is used.  In small functions like this, we often use the register
  28. modifier instead of static.  Just so happens in this one I used
  29. static.
  30.  
  31. >
  32. >>if (len == 0) len = strlen(ptxt);  // Allow null-term strings
  33. >
  34. >If you are writing C code // is illegal syntax - C only supports /* */
  35. >comments. A conforming compiler is required to diagnose //
  36.  
  37. With a flag, we can use this style comment.  Our "style guide" says all
  38. new code should, for readability.  The compiler is "conforming", but lets
  39. you override with a -qcpluscmt flag.
  40.  
  41. >
  42. >>i = strspn(ptxt," \n\t");
  43. >>if (i >= len) return(-1);          // -1 == EMPTY
  44. >>return(i);
  45. >>}
  46. >>
  47. >>
  48. >>The problem is when the buffer is not null-terminated, which
  49. >>happens a lot.  The strspn function COULD read past the end
  50. >>of the buffer.... potentially WAY past it.  Possibly even into
  51. >>memory not owned by the program.
  52. >
  53. >Right, you *must* pass strspn() a pointer to null character terminated string 
  54. >or else the call results in undefined behaviour i.e. anything can happen.
  55. >There's nothing to stop strspn() calling, say, strlen() internally.
  56.  
  57. Yup.  I see your point.  You're the first one to bring up that particular
  58. one.  As I told the others who graciously replied to me, we've done away
  59. with this method and gone back in source code control to the older, less
  60. efficient, but legal, code.  
  61.  
  62. We thought about a malloc each time, but benchmarking showed this slowed the
  63. thing way down, and we tend to avoid mallocs whenever possible - too much
  64. potential for trouble.  For now, the code loops through the buffer in a 
  65. for (or is it while... I forget) loop, checking each byte.
  66.  
  67. Thanks for the comments and your time.
  68. >-----------------------------------------
  69. >Lawrence Kirby | fred@genesis.demon.co.uk
  70. >Wilts, England | 70734.126@compuserve.com
  71. >-----------------------------------------
  72. +============================================================================+
  73. | Steve Mount, Software Engineer            Work: sjjm@hawkeye.idx.com       |
  74. | CIS: 73720,3404  MSN: S_Mountain          Home: mountain@sover.net         |
  75. | AOL: Mountain                                                              |
  76. | WWW: http://www.sover.net/~mountain/      "Fight, Win, Prevail!"           |
  77. +============================================================================+
  78.  
  79.